/*
 * frmRecurso.java
 *
 * Created on 4 de junio de 2008, 03:46 PM
 */

package GUI;
import javax.swing.table.*;
import javax.swing.*;
import AccesoDatos.*;
import ReglasNegocio.*;
import org.jdesktop.swingx.autocomplete.*;
/**
 *
 * @author  dani
 */
public class frmRecurso extends javax.swing.JFrame {

    /** Creates new form frmRecurso */
    public frmRecurso() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.comboActividad.setModel(this.modeloActividades());
        AutoCompleteDecorator.decorate(comboActividad);
        this.refrescarListas();
    }

    private void refrescarListas() {
        
        this.tablaRecurso.setModel(this.modeloRecurso());
        TableColumn col=this.tablaRecurso.getColumnModel().getColumn(2);
        JComboBox comboTabla=new JComboBox(this.modeloActividades());
        
        col.setCellEditor(new DefaultCellEditor(comboTabla));
    }
    
    private DefaultComboBoxModel modeloActividades() {
        DefaultComboBoxModel modelo=new DefaultComboBoxModel();
        String buscar="select * from actividad";
        ConMySQL con=new ConMySQL();
        con.Conectar();
        Vista v=con.crearVista(buscar);
        if (v.getNRegistros()>0) {
            for(int i=0;i<v.getNRegistros();i++) {
                long id=(Long)v.getRegistro(i, 0);
                Actividad a=new Actividad(new Dato("id",String.valueOf(id)));
                modelo.addElement(a);
            }
        }
        return modelo;
    }
    private DefaultTableModel modeloRecurso() {
        DefaultTableModel modelo=new DefaultTableModel();
        
        modelo.addColumn("Id");
        modelo.addColumn("Descripcion");
        modelo.addColumn("Actividad");
        
        Actividad a=(Actividad)this.comboActividad.getSelectedItem();
        String buscar="select * from recurso where fkactividad='"+a.getId()+"'";
        ConMySQL con=new ConMySQL();
        con.Conectar();
        
        Vista v=con.crearVista(buscar);
        if (v.getNRegistros()>0) {
            for(int i=0;i<v.getNRegistros();i++) {
                int id=(Integer)v.getRegistro(i, 0);
                Recurso r=new Recurso(new Dato("id",String.valueOf(id)));
                String[] fila={r.getIdentificador().getValor(),r.getDescripcion(),r.getFkActividad().getNombre()};
                modelo.addRow(fila);
            }
        }
        return modelo;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        comboActividad = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtDescripcion = new javax.swing.JTextField();
        btnRegistrarRecurso = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaRecurso = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Administracio de Recurso");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Registro de Recursos"));

        jLabel1.setText("Actividad");

        comboActividad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboActividadActionPerformed(evt);
            }
        });

        jLabel2.setText("Descripcion");

        btnRegistrarRecurso.setMnemonic('R');
        btnRegistrarRecurso.setText("Registrar");
        btnRegistrarRecurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarRecursoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboActividad, 0, 285, Short.MAX_VALUE)
                            .addComponent(txtDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)))
                    .addComponent(btnRegistrarRecurso, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comboActividad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnRegistrarRecurso)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        tablaRecurso.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tablaRecurso);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnRegistrarRecursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarRecursoActionPerformed
// TODO add your handling code here:
    Recurso r=new Recurso();
    r.setFkActividad((Actividad)this.comboActividad.getSelectedItem());
    r.setDescripcion(this.txtDescripcion.getText());
    r.Crear();
    this.refrescarListas();
    this.txtDescripcion.setText("");
}//GEN-LAST:event_btnRegistrarRecursoActionPerformed

private void comboActividadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboActividadActionPerformed
// TODO add your handling code here:
    this.tablaRecurso.setModel(this.modeloRecurso());
}//GEN-LAST:event_comboActividadActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmRecurso().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRegistrarRecurso;
    private javax.swing.JComboBox comboActividad;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaRecurso;
    private javax.swing.JTextField txtDescripcion;
    // End of variables declaration//GEN-END:variables

}
